$(document).ready(function(){
    ajaxList();
});
$(".search").click(function () {
    var data = $("#searchform").serialize();
    var begin_time = $(".begin_time").val();
    var end_time = $(".end_time").val();
    if(begin_time == '' || end_time == ''){
        dshow('请选择需要查询的时间段');
    }else{
        if(begin_time > end_time){
            dshow('结束时间必须大于开始时间');
        }else{
            ajaxList(data);
        }
    }
});

function ajaxList(data) {
    $.ajax({
        url: '/stray/echartsColumn',
        data: data,
        type: 'POST',
        dataType: 'json',//返回的数据类型
        success: function (data) {
            columnPie(data.result.time, data.result.value, data.result.value1);//柱状图
            $('.stay').html(data.nNum);//待救助流浪狗数量
            $('#stay').html(data.nNumP+'%');//待救助流浪狗占比
            $('.complete').html(data.yNum);//已救助流浪狗数量
            $('#complete').html(data.yNumP+'%');//已救助流浪狗占比
            $('.miss').html(data.mNum);//失踪流浪狗数量
            $('#miss').html(data.mNumP+'%');//失踪流浪狗占比
            $('.sametime').html(data.aNumP+'%');//同时出现多只占比
        }
    })
}

function columnPie(date_time, result, reslut1) {
    var myChart = echarts.init(document.getElementById('main'));
    option = {
        // title: {
        //     text: '流浪狗数量统计',
        //     x: 'center',
        // },
        legend: {
            data:['累计流浪狗数量(主坐标轴)', '新增流浪狗数量(次坐标轴)'],
        },
        dataZoom: [
            {
                show: true,
            },
            {
                type: 'inside',
            },
            {
                show: true,
                yAxisIndex: 0,
                filterMode: 'empty',
                width: 30,
                height: '80%',
                showDataShadow: false,
                left: '93%'
            }
        ],
        xAxis: {
            type: 'category',
            data: date_time,
        },
        yAxis: {
            type: 'value'
        },
        series: [{
            name:'累计流浪狗数量(主坐标轴)',
            data: result,
            type: 'bar',
            itemStyle:{
                normal:{
                    color:'#FF9966',
                    label : {
                        show: true,
                        position: 'top'
                    }
                }
            },
        },
        {
            name:'新增流浪狗数量(次坐标轴)',
            type:'line',
            itemStyle : { 
                normal : {
                    color:'blue',
                    label : {show: true}
                }
            },
            data:reslut1
        }]
    };
    myChart.setOption(option);
}
$("#province").on("change",function () {
    var city = $("#province").val();
    $("#area").html("");
    var html = " <option value=''>全部</option>";
    $("#area").append(html);
    getCity(city);
})
$("#city").on("change",function () {
    var city = $("#city").val();
    getArea(city);
})
function getCity(city){
    $.ajax({
        url: '/stray/city',
        data: {city: city},
        type: 'POST',
        dataType: 'json',//返回的数据类型
        success: function (data) {
            var cityArray =  data.cityArray;
            $("#city").html("");
            var html = " <option value=''>全部</option>";
            $.each(cityArray, function (i, item) {
                html += "<option value='"+item.district_id+"'>" + item.district + "</option>";
            });
            $("#city").append(html);
        }
    })
}

function getArea(area){
    $.ajax({
        url: '/stray/area',
        data: {area: area},
        type: 'POST',
        dataType: 'json',//返回的数据类型
        success: function (data) {
            var cityArray =  data.cityArray;
            $("#area").html("");
            var html = " <option value=''>全部</option>";
            $.each(cityArray, function (i, item) {
                html += "<option value='"+item.district_id+"'>" + item.district + "</option>";
            });
            $("#area").append(html);
        }
    })
}